package dolmisani.toys.emulators.j2600.cart;

public class Cartridge4K extends Cartridge {

    private final static long serialVersionUID = -3240308689643904765L;
    
    private final static int CARTRIDGE_MASK_VALUE = 0x0FFF;

    private final static int CARTRIDGE_SIZE = 4096;
    private final static int CARTRIDGE_BANK_COUNT = 1;

    public Cartridge4K(byte[] imageBytes) {
        
        if(imageBytes.length != CARTRIDGE_SIZE) {
            throw new RuntimeException(
                    String.format("Invalid cartridge size %d", imageBytes.length)
            );
        }
        
        md5Digest = calculateMD5Digest(imageBytes);
        cartridgeImage = toIntArray(imageBytes);
    }

    @Override
    public void install(jstella.core.JSSystem system) {
        this.system = system;
        
        // Map ROM image into the system
        addDirectPeekAccess(0x1000, 0x2000, cartridgeImage, CARTRIDGE_MASK_VALUE);  
    }

    @Override
    public int peek(int address) {
        return cartridgeImage[address & CARTRIDGE_MASK_VALUE];
    }

    @Override
    public void poke(int address, int value) {
        // This is a ROM 
    }

    @Override
    protected void setCurrentBank(int bank) {
        // Doesn't support bankswitching
    }

    @Override
    protected int getCurrentBank() {
        // Doesn't support bankswitching
        return 0;
    }

    @Override
    protected int bankCount() {
        return CARTRIDGE_BANK_COUNT;
    }

    @Override
    public boolean patch(int address, int value) {
        cartridgeImage[address & CARTRIDGE_MASK_VALUE] = value;
        return true;
    }
}
